package com.fzfnn.scrapbook.service.impl;

import com.fzfnn.scrapbook.entity.Journal;
import com.fzfnn.scrapbook.entity.User;
import com.fzfnn.scrapbook.mapper.JournalLikeMapper;
import com.fzfnn.scrapbook.mapper.UserMapper;
import com.fzfnn.scrapbook.service.JournalLikeService;
import com.fzfnn.scrapbook.util.R;
import com.fzfnn.scrapbook.vo.JournalVo;
import com.fzfnn.scrapbook.vo.UserinformationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class JournalLikeServiceImpl implements JournalLikeService {

    @Autowired
    private JournalLikeMapper likeMapper;
    @Autowired
    private RedisTemplate<String, String> redisMail;
    @Autowired
    private JournalLikeMapper journalLikeMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public R likeJournal(int journalId, String token) {
        String userIds = redisMail.opsForValue().get("token:" + token);
        if (userIds == null){
            return R.errorMsg("登录信息无效");
        }
        Journal journal = likeMapper.selectById(journalId);
        if (journal == null) {
            return R.errorMsg("该手帐不存在");
        }
        int  userId = Integer.parseInt(userIds);
        if (likeMapper.isLiked(journalId, userId) > 0) {
            return R.errorMsg("不能重复点赞");
        }
        likeMapper.insertLike(journalId, userId);
        likeMapper.incrementLikeCount(journalId);
        return R.successMsg("点赞成功");
    }

    @Override
    public R unlikeJournal(int journalId, String token) {
        String userIds = redisMail.opsForValue().get("token:" + token);
        if (userIds == null){
            return R.errorMsg("登录信息无效");
        }
        Journal journal = likeMapper.selectById(journalId);
        if (journal == null) {
            return R.errorMsg("该手帐不存在");
        }
        int  userId = Integer.parseInt(userIds);
        if (likeMapper.isLiked(journalId, userId) == 0) {
            return R.errorMsg("你未点赞");
        }
        likeMapper.deleteLike(journalId, userId);
        likeMapper.decrementLikeCount(journalId);
        return R.successMsg("取消点赞成功");
    }

    @Override
    public R getLikeCount(int journalId) {
        Journal journal = likeMapper.selectById(journalId);
        if (journal == null) {
            return R.errorMsg("该手帐不存在");
        }
        int count = likeMapper.countLikes(journalId);
        return R.success("点赞数", count);
    }

    @Override
    public R isLiked(int journalId, String token) {
        String userIds = redisMail.opsForValue().get("token:" + token);
        if (userIds == null){
            return R.errorMsg("登录信息无效");
        }
        Journal journal = likeMapper.selectById(journalId);
        if (journal == null) {
            return R.errorMsg("该手帐不存在");
        }
        int  userId = Integer.parseInt(userIds);
        boolean liked = likeMapper.isLiked(journalId, userId) > 0;
        return R.success("判断点赞", liked);
    }

    @Override
    public R getLikedUsers(String token, int journalId) {
        String userIdStr = redisMail.opsForValue().get("token:" + token);
        if (userIdStr == null) {
            return R.errorMsg("登录信息无效");
        }

        // 查询点赞该日记的用户 ID 列表
        List<Integer> userIdList = journalLikeMapper.selectLikedUsers(journalId);

        List<UserinformationVo> result = new ArrayList<>();
        for (Integer id : userIdList) {
            User user = userMapper.selectUserByuserid(id);
            if (user != null) {
                UserinformationVo vo = new UserinformationVo();
                vo.setUserId(user.getUserId());
                vo.setUsername(user.getUsername());
                vo.setUserAvatar(user.getImgurl());
                vo.setEmail(user.getEmail());
                result.add(vo);
            }
        }
        if (result.isEmpty()){
            return R.errorMsg("还没有点赞用户");
        }
        return R.successData(result);
    }

    @Override
    public R getLikedJournals(String token) {
        String userIdStr = redisMail.opsForValue().get("token:" + token);
        if (userIdStr == null) {
            return R.errorMsg("登录信息无效");
        }
        int userId = Integer.parseInt(userIdStr);
        List<JournalVo> journals = journalLikeMapper.getLikedJournalsByUserId(userId);
        return R.successData(journals);
    }


}
